package com.dc.business.datacollectbusiness.service.impl;

import com.dc.business.datacollectbusiness.entity.Company;
import com.dc.business.datacollectbusiness.entity.Factories;
import com.dc.business.datacollectbusiness.mapper.CompanyMapper;
import com.dc.business.datacollectbusiness.mapper.FactoriesMapper;
import com.dc.business.datacollectbusiness.service.FactoriesService;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class FactoriesServiceImpl implements FactoriesService {
    @Autowired
    private FactoriesMapper factoriesMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public List<Factories> getAllFactoriesByCompanyId(Integer companyId) {
        Factories last = null;
        List<Factories> factories = factoriesMapper.selectAllFactoryByCompanyId(companyId);
        System.out.println(factories);
        Iterator<Factories> iterator = factories.iterator();
        while (iterator.hasNext()) {
            Factories element = iterator.next();
            if (element.getFactoryName().equals("全校电表")) {
                last = element;
                iterator.remove();
            }
        }
        factories.add(last);
        return factories;
    }

    @Override
    public boolean deleteFactoriesById(int factoryId) {
        return factoriesMapper.deleteFactoriesById(factoryId);
    }

    @Override
    public boolean insertFactories(Factories factories) {
        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        factories.setCreateBy(map.get("username").toString());
        Company company=new Company();
        company.setCompanyId(factories.getCompanyId());
        companyMapper.updateCompany2(company);

        return factoriesMapper.insertFactories(factories);
    }

    @Override
    public boolean updateFactories(Factories factories) {

        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        factories.setUpdateBy(map.get("username").toString());

        return factoriesMapper.updateFactories(factories);
    }
}
